package {{.TableDetailResp.PackageName}}
  {{ $moduleNameUp := FirstLetterUp .TableDetailResp.ModuleName}}
{{$shortStructName := .TableDetailResp.StructName}}
{{if not .IgnoreTablePrefix}}{{$shortStructName = ReplaceStrInsensitive .TableDetailResp.StructName .TableDetailResp.TablePrefix ""}}{{end}}
{{- $lowerShortStructName := FirstLetterLower $shortStructName}}
import (
	"MoSkeleton/framework/core/consts"
	"MoSkeleton/framework/core/dto/response"
	"MoSkeleton/framework/utils"
	"MoSkeleton/global"
	"MoSkeleton/services"
	s1 "MoSkeleton/web/pogos/request/{{.TableDetailResp.PackageName}}"
	"MoSkeleton/web/v1/http/base"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"strconv"
)

type {{.TableDetailResp.StructName}}Ctrl struct {
	base.BaseCtrl
}

func (s *{{.TableDetailResp.StructName}}Ctrl) GetCtrlTitle() string {
	return "{{.TableDetailResp.TableComment}}"
}

// Add{{.TableDetailResp.StructName}} 添加{{.TableDetailResp.TableComment}}
// @Tags {{.TableDetailResp.TableComment}}管理
// @Summary 添加{{.TableDetailResp.TableComment}}
// @Security ApiKeyAuth
// @Param data body s1.{{.TableDetailResp.StructName}}AddReq true "{{.TableDetailResp.TableComment}}信息"
// @Success 200 {object} response.Response{data=s1.{{.TableDetailResp.StructName}}AddReq} "{"code":200,"data":{},"msg":"添加成功"}"
// @Router /{{.TableDetailResp.PackageName}}/{{$lowerShortStructName}} [post]
func (s *{{.TableDetailResp.StructName}}Ctrl) Add{{$shortStructName}}(c *gin.Context) {
	addReq := &s1.{{.TableDetailResp.StructName}}AddReq{}
	if err := c.ShouldBindJSON(addReq); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsg(c, consts.ValidatorParamsCheckFailCode, "参数错误")
		return
	}
	{{if ContainField  .TableDetailResp.Columns "created_by"    }}
	addReq.CreatedBy = utils.GetUsernameFromGin(c)
	{{end}}
	{{if ContainField  .TableDetailResp.Columns "updated_by"}}
	addReq.UpdatedBy = addReq.CreatedBy
	{{end}}
	if ok, err := services.ServiceGroupInst.{{$moduleNameUp}}GroupInst.{{.TableDetailResp.StructName}}ServiceInst.Add{{$shortStructName}}(addReq); err != nil {
		global.MoLogger.Error("添加{{.TableDetailResp.TableComment}}失败", zap.Error(err))
		response.FailErrorWithCode(c, err, consts.ServerOccurredErrorCode, "添加{{.TableDetailResp.TableComment}}失败")
		return
	} else {
		if ok {
			response.Success(c, "添加成功", nil)
			return
		} else {
			response.FailMsg(c, consts.ServerOccurredErrorCode, "添加失败")
		}
	}
}

// DeleteById 根据id删除{{.TableDetailResp.TableComment}}
// @Tags {{.TableDetailResp.TableComment}}管理
// @Summary 根据id删除{{.TableDetailResp.TableComment}}
// @Security ApiKeyAuth
// @Param id path string true "{{.TableDetailResp.TableComment}}id"
// @Success 200 {object} response.Response{data=s1.{{.TableDetailResp.StructName}}AddReq} "{"code":200,"data":{},"msg":"删除成功"}"
// @Router /{{.TableDetailResp.PackageName}}/{{$lowerShortStructName}}/{id} [delete]
func (s *{{.TableDetailResp.StructName}}Ctrl) DeleteByIds(c *gin.Context) {
	if ids, err := utils.TextToUint64s(c.Param("ids"), ","); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsg(c, consts.ValidatorParamsCheckFailCode, "参数错误")
		return
	} else {
		if ok, err := services.ServiceGroupInst.{{$moduleNameUp}}GroupInst.{{.TableDetailResp.StructName}}ServiceInst.Delete{{$shortStructName}}ByIds(c, ids); err != nil {
			global.MoLogger.Error("删除{{.TableDetailResp.TableComment}}失败", zap.Error(err))
			response.FailErrorWithCode(c, err, consts.CommDeleteFailCode, "删除{{.TableDetailResp.TableComment}}失败")
			return
		} else if !ok {
			response.FailMsg(c, consts.ServerOccurredErrorCode, "删除失败")
			return
		} else {
			response.Success(c, "删除成功", nil)
			return
		}
	}
}

// Update{{$shortStructName}}ById 更新{{.TableDetailResp.TableComment}}
// @Tags {{.TableDetailResp.TableComment}}管理
// @Summary 更新{{.TableDetailResp.TableComment}}
// @Security ApiKeyAuth
// @Param data body s1.{{.TableDetailResp.StructName}}UpdateReq true "{{.TableDetailResp.TableComment}}信息"
// @Success 200 {object} response.Response{data=s1.{{.TableDetailResp.StructName}}AddReq} "{"code":200,"data":{},"msg":"更新成功"}"
// @Router /{{.TableDetailResp.PackageName}}/{{$lowerShortStructName}} [put]
func (s *{{.TableDetailResp.StructName}}Ctrl) Update{{$shortStructName}}ById(c *gin.Context) {
	updateReq := s1.{{.TableDetailResp.StructName}}UpdateReq{}
	if err := c.ShouldBindJSON(&updateReq); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsg(c, consts.ValidatorParamsCheckFailCode, "参数错误")
		return
	}
    {{if ContainField  .TableDetailResp.Columns "updated_by"}}
	updateReq.UpdatedBy = utils.GetUsernameFromGin(c)
	{{end}}
	if ok, err := services.ServiceGroupInst.{{$moduleNameUp}}GroupInst.{{.TableDetailResp.StructName}}ServiceInst.Update{{$shortStructName}}ById(c, &updateReq); err != nil {
		global.MoLogger.Error("更新{{.TableDetailResp.TableComment}}失败", zap.Error(err))
		response.FailErrorWithCode(c, err, consts.CommUpdateFailCode, "更新{{.TableDetailResp.TableComment}}失败")
	} else if !ok {
		response.FailMsg(c, consts.ServerOccurredErrorCode, "更新失败")
	} else {
		response.Success(c, "更新成功", nil)
	}
}

{{if ContainField  .TableDetailResp.Columns "status"}}
// Update{{$shortStructName}}Status 更新{{.TableDetailResp.TableComment}}状态
// @Tags {{.TableDetailResp.TableComment}}管理
// @Summary 更新{{.TableDetailResp.TableComment}}状态
// @Security ApiKeyAuth
// @Param data body s1.Change{{.TableDetailResp.StructName}}StatusReq true "{{.TableDetailResp.TableComment}}信息"
// @Success 200 {object} response.Response{data=s1.{{.TableDetailResp.StructName}}AddReq} "{"code":200,"data":{},"msg":"更新成功"}"
// @Router /{{.TableDetailResp.PackageName}}/{{$lowerShortStructName}}/changeStatus [put]
func (s *{{.TableDetailResp.StructName}}Ctrl) Update{{$shortStructName}}Status(c *gin.Context) {
	req := s1.Change{{.TableDetailResp.StructName}}StatusReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsg(c, consts.ValidatorParamsCheckFailCode, "参数错误")
		return
	}
	req.UpdatedBy = utils.GetUsernameFromGin(c)
	if ok, err := services.ServiceGroupInst.{{$moduleNameUp}}GroupInst.{{.TableDetailResp.StructName}}ServiceInst.Update{{$shortStructName}}Status(c, &req); err != nil {
		global.MoLogger.Error("更新{{.TableDetailResp.TableComment}}状态失败", zap.Error(err))
		response.FailErrorWithCode(c, err, consts.CommUpdateFailCode, "更新{{.TableDetailResp.TableComment}}状态失败")
		return
	} else if !ok {
		response.FailMsg(c, consts.ServerOccurredErrorCode, "更新失败")
		return
	}
	response.Success(c, "更新成功", nil)
}
{{end}}

// Get{{$shortStructName}}Page 获取{{.TableDetailResp.TableComment}}列表
// @Tags {{.TableDetailResp.TableComment}}管理
// @Summary 获取{{.TableDetailResp.TableComment}}列表
// @Security ApiKeyAuth
// @Param data query s1.{{.TableDetailResp.StructName}}PageReq true "{{.TableDetailResp.TableComment}}信息"
// @Success 200 {object} response.Response{data=response.PageResult} "{"code":200,"data":{},"msg":"获取成功"}"
// @Router /{{.TableDetailResp.PackageName}}/{{$lowerShortStructName}}/list [get]
func (s *{{.TableDetailResp.StructName}}Ctrl) Get{{$shortStructName}}Page(c *gin.Context) {
	pageReq := &s1.{{.TableDetailResp.StructName}}PageReq{}
	if err := c.ShouldBindQuery(pageReq); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsg(c, consts.ValidatorParamsCheckFailCode, "参数错误")
		return
	}
	pageReq.ShouldBindParams(c)
	if pageResp, err := services.ServiceGroupInst.{{$moduleNameUp}}GroupInst.{{.TableDetailResp.StructName}}ServiceInst.Get{{$shortStructName}}Page(pageReq); err != nil {
		global.MoLogger.Error("获取{{.TableDetailResp.TableComment}}列表失败", zap.Error(err))
		response.FailErrorWithCode(c, err, consts.CommSelectFailCode, "获取{{.TableDetailResp.TableComment}}列表失败")
		return
	} else {
		response.Success(c, "获取成功", pageResp)
		return
	}
}

// Get{{$shortStructName}}ById 根据id获取{{.TableDetailResp.TableComment}}信息
// @Tags {{.TableDetailResp.TableComment}}管理
// @Summary 根据id获取{{.TableDetailResp.TableComment}}信息
// @Security ApiKeyAuth
// @Param id path string true "{{.TableDetailResp.TableComment}}id"
// @Success 200 {object} response.Response{data=s1.{{.TableDetailResp.StructName}}AddReq} "{"code":200,"data":{},"msg":"获取成功"}"
// @Router /{{.TableDetailResp.PackageName}}/{{$lowerShortStructName}}/{id} [get]
func (s *{{.TableDetailResp.StructName}}Ctrl) Get{{$shortStructName}}ById(c *gin.Context) {
	idStr := c.Param("id")
	if id, err := strconv.ParseUint(idStr, 10, 64); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsg(c, consts.ValidatorParamsCheckFailCode, "参数错误")
		return
	} else {
		if {{$lowerShortStructName}}, err := services.ServiceGroupInst.{{$moduleNameUp}}GroupInst.{{.TableDetailResp.StructName}}ServiceInst.Get{{$shortStructName}}ById(c, id); err != nil {
			global.MoLogger.Error("获取{{.TableDetailResp.TableComment}}失败", zap.Error(err))
			response.FailErrorWithCode(c, err, consts.CommSelectFailCode, "获取{{.TableDetailResp.TableComment}}失败")
			return
		} else {
			if {{$lowerShortStructName}} == nil {
				response.FailMsg(c, consts.ServerOccurredErrorCode, "获取{{.TableDetailResp.TableComment}}失败")
			} else {
				response.Success(c, "获取成功", {{$lowerShortStructName}})
			}
		}
	}
}